<template>
  <div class="detail-wrapper">
    <el-page-header @back="goBack" content="物料详情"> </el-page-header>
    <!-- <el-divider></el-divider> -->
    <div class="topBox paddingBorder">
      <el-descriptions
        class="margin-top"
        :title="dataForm.name"
        :column="3"
        :labelStyle="labelStyle"
        :contentStyle="contentStyle"
        border
      >
        <template slot="extra">
          <el-button type="primary" size="small" @click="toEdit"
            >编辑</el-button
          >
          <el-button type="primary" size="small" @click="handleDelete"
            >删除</el-button
          >
        </template>
        <el-descriptions-item>
          <template slot="label"> 创建人 </template>
          {{ dataForm.operator_user }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 创建时间 </template>
          {{ dataForm.operator_user }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 最后修改人 </template>
          {{ dataForm.operator_user }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 修改时间 </template>
          {{ dataForm.operator_user }}
        </el-descriptions-item>
      </el-descriptions>
    </div>

    <div class="top36 prodBox paddingBorder">
      <div class="titleLabel">物料信息</div>
      <el-descriptions
        class="margin-top top36"
        title="基本信息"
        :column="3"
        :labelStyle="labelStyle"
        :contentStyle="contentStyle"
        border
      >
        <el-descriptions-item>
          <template slot="label"> 物料编号 </template>
          {{ dataForm.material_serial }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 物料名称 </template>
          {{ dataForm.name }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 物料类别 </template>
          {{ $getDictLabel('material_type', String(dataForm.material_type)) }}
        </el-descriptions-item>
        <el-descriptions-item :span="3">
          <template slot="label"> 物料属性 </template>
          {{ $getDictLabel('material_attr', String(dataForm.material_attr)) }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 物料图片 </template>
          <div class="prodImgBox horizontalCenter">
            <div
              class="imgItem"
              v-for="(item, index) in fileList"
              :key="index"
              @click="handlePreview(item.url)"
            >
              <img :src="item.url" />
            </div>
          </div>
        </el-descriptions-item>
      </el-descriptions>
    </div>
    <el-dialog :visible.sync="dialogVisible" title="查看">
      <img width="100%" :src="dialogImageUrl" class="avatar" />
    </el-dialog>
  </div>
</template>

<script>
import { addDynamicRoute } from '@/router'
import { getMaterialDetail, deleteMaterial } from '@/api'
export default {
  data() {
    return {
      dataForm: {},
      dialogImageUrl: '',
      dialogVisible: false,
      fileList: [],
      labelStyle: {
        width: '150px',
      },
      contentStyle: {
        width: '28vw',
      },
    }
  },
  computed: {},
  activated() {
    const id = this.$route.params.id
    this.dataForm.id = id
    if (id) this.fetchDetail(id)
  },
  methods: {
    async handleDelete() {
      this.$confirm('此操作将删除该物料, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          const res = await deleteMaterial({ id: this.dataForm.id })
          if (res?.status === 1) {
            this.$message.success(res?.msg || '')
            this.$router.go(-1)
          }
        })
        .catch(() => {})
    },
    toEdit() {
      const routeParams = {
        routeName: `product-material__add`,
        title: `物料清单 - 编辑`,
        path: 'product/material-add',
        params: {
          id: this.dataForm.id || '',
        },
      }
      addDynamicRoute(routeParams, this.$router)
    },
    async fetchDetail(id) {
      const params = {
        id,
      }
      const res = await getMaterialDetail(params)
      console.log(res)
      if (res?.status === 1) {
        const data = res?.data || {}
        this.dataForm = data
        const pics = data?.pics
        if (pics.length > 0) {
          const arr = []
          pics.forEach((item) => {
            arr.push({ name: '', url: item || '' })
          })
          this.fileList = arr
        }
      }
    },
    goBack() {
      this.$router.go(-1)
    },
    handlePreview(url) {
      this.dialogImageUrl = url
      this.dialogVisible = true
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        if (rowIndex % 2 === 0) {
          return {
            rowspan: 2,
            colspan: 1,
          }
        } else {
          return {
            rowspan: 0,
            colspan: 0,
          }
        }
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.detail-wrapper {
  background: #fff;
  padding: 20px;
  ::v-deep .el-page-header__title,
  .el-page-header__content {
    font-size: 12px;
  }

  ::v-deep .el-page-header__content {
    font-size: 12px;
  }

  .topBox {
    margin-top: 36px;
    ::v-deep .el-descriptions__title {
      font-size: 20px;
      font-weight: 600;
      position: relative;
      padding-left: 16px;
      color: #303133;
      &::before {
        background: $mainColor;
        content: '';
        display: inline-block;
        height: 20px;
        left: 0;
        position: absolute;
        top: calc(50% - 10px);
        width: 3px;
      }
    }
  }

  .prodBox {
    .prodImgBox {
      .imgItem {
        width: 64px;
        height: 64px;
        border: 1px solid #ccc;
        margin-left: 12px;
        cursor: pointer;
        &:first-child {
          margin-left: 0;
        }
        img {
          width: 100%;
          height: 100%;
        }
      }
    }
  }
}
</style>

<style lang="scss">
::v-deep .el-page-header__title {
  font-size: 12px;
}
</style>
